package com.github.prontera.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RequestLogging;
import com.github.prontera.Shift;
import com.github.prontera.StatusCode;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.common.filter.IgnoreAuth;
import com.github.prontera.domain.Client;
import com.github.prontera.domain.Coterie;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.CoterieService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


/**
 * 友圈
 */
@RestController
@Api(value = "圈子")
@RequestMapping(value = ApiVersion.API_V1_PREFIX+"/coterie")
public class CoterieController{

	@Autowired
	private CoterieService coterieService;
	
	@RequestMapping(value = "findMyCoterie", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量获取用户自己发布得圈子",notes = "增量获取用户自己发布得圈子")
	public ObjectDataResponse<List<Coterie>> findMyCoterie(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Coterie coterie){
		if(coterie.getEndTime_()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "开始时间必传!");
		}
		if(coterie.getLimitStart()==null||coterie.getPageSize()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "分页参数必传!");
		}
		coterie.setOrderBy("C_COTERIE.create_time desc");
		coterie.setClientId(clientId);
		List<Coterie> datas=coterieService.find(coterie);
		return new ObjectDataResponse<List<Coterie>>(datas);
	}
	
	
	@IgnoreAuth
	@RequestLogging
	@RequestMapping(value = "findClientfCoterie", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量获取用户发布得圈子",notes = "增量获取用户自己发布得圈子")
	public ObjectDataResponse<List<Coterie>> findClientfCoterie(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Coterie coterie){
		if(coterie.getEndTime_()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "开始时间必传!");
		}
		if(coterie.getLimitStart()==null||coterie.getPageSize()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "分页参数必传!");
		}
		coterie.setFllowClientId(clientId);
		coterie.setOrderBy("C_COTERIE.create_time desc");
		List<Coterie> datas=coterieService.find(coterie);
		return new ObjectDataResponse<List<Coterie>>(datas);
	}
	
	@IgnoreAuth
	@RequestLogging
	@RequestMapping(value = "findCoterie", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量获取用户圈子",notes = "增量获取用户圈子")
	public ObjectDataResponse<List<Coterie>> findCoterie(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Coterie coterie){
//		if(coterie.getEndTime_()==null){
//			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "开始时间必传!");
//		}
		if(coterie.getLimitStart()==null||coterie.getPageSize()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "分页参数必传!");
		}
		coterie.setOrderBy("C_COTERIE.create_time desc");
		List<Coterie> datas=coterieService.findCoterie(coterie);
		return new ObjectDataResponse<List<Coterie>>(datas);
	}
	
	@IgnoreAuth
	
	@RequestMapping(value = "findFollowCoterie", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "增量获取用户关注过得圈子",notes = "增量获取用户关注过得圈子")
	public ObjectDataResponse<List<Coterie>> findFollowCoterie(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Coterie coterie){
		coterie.setClientId(clientId);
		coterie.setOrderBy("C_COTERIE.create_time desc");
		List<Coterie> datas=coterieService.findFollowCoterie(coterie);
		return new ObjectDataResponse<List<Coterie>>(datas);
	}
	
	
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户发布圈子",notes = "用户发布圈子")
	public ObjectDataResponse<Coterie> insert(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Coterie coterie){
		coterie.setClientId(clientId);
		
		Client client=new Client();
		client.setId(clientId);
		client.setCoterieCount(1);
		
		if(!coterieService.insertCoterie(coterie, client)){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "网络错误,请重新提交数据!");
		}
//		cacheService.insertCoterie(coterie);
		return new ObjectDataResponse<Coterie>(coterie);
	}
	
	
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "用户删除发布的圈子",notes = "用户删除发布的圈子")
	public ObjectDataResponse<Coterie> remove(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long clientId,@RequestBody Coterie coterie){
		if(coterie.getId()==null){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "id必传!");
		}
		coterie.setClientId(clientId);
		
		Client client=new Client();
		client.setId(clientId);
		client.setCoterieCount(-1);
		
		if(!coterieService.removeCoterie(coterie,client)){
			Shift.fatal(StatusCode.INVALID_PARAMS_CONVERSION, "网络错误,请重新提交数据!");
		}
//		Coterie removeCoterie=coterieService.get(coterie.getId());
//		cacheService.removeCoterie(removeCoterie);
		return new ObjectDataResponse<Coterie>(coterie);
	}
	
}
